package com.magnificent.panda.custom;

import com.magnificent.panda.model.entity.FlowEvent;
import com.magnificent.panda.utils.JFreeChartHelper;
import com.magnificent.panda.utils.PandaConstants;
import com.silverhaze.commons.helpers.CommonUIHelper;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYDataset;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

/**
 * User: Babanin
 * Date: 01.02.11
 * Time: 9:00
 */
public class CorrelationChart extends JDialog {

    private JFreeChart correlationChart;
    private ChartPanel correlationChartPanel;

    public CorrelationChart() {
        initializeControls();
        initializeView();
    }

    private void initializeView() {
        setLayout(new BorderLayout());
        getRootPane().setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setTitle(PandaConstants.PANDA_TITLE + " - Моделирование");


        add(CommonUIHelper.packInShiftedPanel(correlationChartPanel, 5, 5, 0, 0), BorderLayout.CENTER);

        add(
                CommonUIHelper.createBorderLayoutPanel(new JComponent[]{
                        CommonUIHelper.hBox(),
                        new JButton(new AbstractAction("Закрыть") {
                            public void actionPerformed(ActionEvent e) {
                                CorrelationChart.this.dispose();
                            }
                        })
                }, new Object[]{
                        BorderLayout.LINE_START, BorderLayout.LINE_END
                }), BorderLayout.SOUTH);

        pack();
        setLocationRelativeTo(null);
    }

    private void initializeControls() {
        correlationChart = JFreeChartHelper.newCorrelationChart(JFreeChartHelper.createFlowDataset(new ArrayList<FlowEvent>()), "", "", "");

        correlationChartPanel = new ChartPanel(correlationChart);
        correlationChartPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    }

    private void updateGraph(double[] correlation, double[] correlationMixed) {
        XYDataset dataset = JFreeChartHelper.createDataset(correlation, correlationMixed);

        correlationChart.getXYPlot().setDataset(0, dataset);
    }

    public static void main(String[] args) {
        CorrelationChart chart = new CorrelationChart();




//        chart.updateGraph(new double[]{0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.5, 0.5, 0.5}, new double[0]);
//        chart.updateGraph(new double[]{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0}, new double[0]);
        chart.updateGraph(new double[]{0, 1, 1, 1, 1, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 1, 1}, new double[0]);
        chart.setVisible(true);
    }
}
